<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

require_once JPATH_BASE .DS. 'components' .DS. 'com_mariageplanner' .DS. 'helpers' .DS. 'modeladmin.php';

/**
 * Task Model
 */
class MariagePlannerModelTask extends MariagePlannerBaseModelAdmin
{	
	protected $_table_name = 'tasks';

	public function getTable($type = 'Task', $prefix = 'MariagePlannerTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_mariageplanner.task', 'task', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}
	
		return $form;
	}
	
	public function getScript()
	{
		return 'components/com_mariageplanner/models/forms/task.js';
	}
	
	protected function loadFormData()
	{
		$data = parent::loadFormData();
		
		if(!empty($data->date)) {
			$date = JFactory::getDate($data->date)->toUnix(); 
			$time = date("H:i", $date);
			$data->time = $time;
		}
		return $data;
	}
	
	public function save($data)
	{		
		$time = strtotime($data['time'].' GMT', 0);
		$date = strtotime($data['date']);
		$date = date('Y-m-d H:i:s', $date + $time);
		$data["date"] =  $date;
		
		return parent::save($data);
	}
	
	public function done()
	{
		if(!$this->isOwner())
			return false;
		
		$id = $this->getState('item.id');
		
		$table = $this->getTable();
		$table->load($id);
		$table->state = 1;
		
		// Store the data.
		if (!$table->store())
		{
			$this->setError($table->getError());
			return false;
		}
		return true;
	}
}
?>